package com.julis.activity.check;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.method.ReplacementTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.Constant;
import com.baseutils.SpfUtils;
import com.baseutils.TimeUtils;
import com.com.handy.function.qrcode.Contextkay;
import com.com.handy.function.qrcode.ScanConfig;
import com.com.handy.function.qrcode.ScanLauncher;
import com.github.czy1121.view.RoundButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.julis.R;
import com.julis.ui.horizon.HorizontalProgressViewModel;
import com.julis.ui.horizon.Node;

/**
 * 输入信息
 */
public class N_Car_VIN1_Activity extends AppCompatActivity {
    public static final String KEY_TITLE = "key_title";
    public static final String KEY_IS_CONTINUOUS = "key_continuous_scan";
    @BindView(R.id.img_back)
    RelativeLayout imgBack;

    @BindView(R.id.textView13)
    TextView textView13;
    @BindView(R.id.textView23)
    TextView textView23;
    @BindView(R.id.vin_code)
    EditText vin;


    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.bt_chongxin)
    RoundButton btChongxin;
    @BindView(R.id.bt_queding)
    RoundButton btQueding;
    @BindView(R.id.yincang)
    LinearLayout yincang;
    @BindView(R.id.layout_pinpai)
    LinearLayout layout_pinpai;
    @BindView(R.id.layout_xinghao)
    LinearLayout layout_xinghao;
    boolean Inpue_Obu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_vin);
        ButterKnife.bind(this);
        Contextkay.key = 0;
        Inpue_Obu = (boolean) SpfUtils.get(this, Constant.Inpue_Obu, true);
        layout_xinghao.setVisibility(View.GONE);
        layout_pinpai.setVisibility(View.GONE);
        vin.setTransformationMethod(new ReplacementTransformationMethod() {
            @Override
            protected char[] getOriginal() {
                char[] originalCharArr = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
                return originalCharArr;
            }

            @Override
            protected char[] getReplacement() {
                char[] replacementCharArr = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
                return replacementCharArr;
            }
        });

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        // 引用
        HorizontalProgressViewModel model = new HorizontalProgressViewModel();
        model.setViewUp(this, recyclerView, getProgressList());
    }

    /**
     * 模拟节点数据
     * node1.nodeStatus: 0 已完成状态  1正在处理状态  -1待处理状态
     *
     * @return
     */
    private List<Node> getProgressList() {
        List<Node> list = new ArrayList<>();
        if (Inpue_Obu) {

            Node node1 = new Node();
            Node node2 = new Node();
            Node node4 = new Node();
            node1.nodeName = "录入VIN";
            node1.nodeStatus = 1;
            node2.nodeName = "录入OBU";
            node2.nodeStatus = -1;
            node4.nodeName = "设备检测";
            node4.nodeStatus = -1;
            list.add(node1);
            list.add(node2);
            list.add(node4);
        } else {
            Node node1 = new Node();
            Node node4 = new Node();
            node1.nodeName = "录入VIN";
            node1.nodeStatus = 1;
            node4.nodeName = "设备检测";
            node4.nodeStatus = -1;
            list.add(node1);
            list.add(node4);
        }


        return list;

    }

    Intent intent;
    private boolean isContinuousScan;
    String shju;

    @OnClick({R.id.img_back, R.id.bt_chongxin, R.id.bt_queding,})
    public void onViewClicked(View view) {
        isContinuousScan = false;
        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.in, R.anim.out);
        switch (view.getId()) {

            case R.id.img_back:
                finish();
                break;
            case R.id.bt_chongxin:
//                isContinuousScan = true;
//                intent = new Intent(this, N_Car_VINEWMActivity.class);
//                intent.putExtra(KEY_TITLE, "自定义扫码");
//                intent.putExtra(KEY_IS_CONTINUOUS, isContinuousScan);
//                ActivityCompat.startActivityForResult(this, intent, REQUEST_CODE_SCAN, optionsCompat.toBundle());
                ScanConfig.KEY_DECODE_1D_INDUSTRIAL = true;
                ScanConfig.KEY_DECODE_1D_PRODUCT = true;
                ScanConfig.KEY_AUTO_ORIENTATION = true;

                new ScanLauncher().startSingle(N_Car_VIN1_Activity.this, result -> {
//                    ((TextView) findViewById(R.id.result)).setText("扫描结果: \n" + result);
                    yincang.setVisibility(View.VISIBLE);
                    layout_xinghao.setVisibility(View.GONE);
                    layout_pinpai.setVisibility(View.GONE);
                    shju = result;


                        vin.setText(result.toUpperCase());


                });
                break;
            case R.id.bt_queding:
                shju = vin.getText().toString().toUpperCase();
                if (TextUtils.isEmpty(shju)) {
                    Toast.makeText(this, "VIN号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (shju.length() != 17) {
                    Toast.makeText(this, "请输入正确的VIN号", Toast.LENGTH_SHORT).show();
                    return;
                }
                new AlertDialog.Builder(N_Car_VIN1_Activity.this).setMessage("请核对扫描到的VIN码与车辆的VIN码是否一致?")
                        .setCancelable(false)
                        .setNegativeButton("不一致", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }).setPositiveButton("一致", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String gettime = TimeUtils.gettime();
                        SpfUtils.put(N_Car_VIN1_Activity.this, Constant.VIN_InputTime, gettime);
                        SpfUtils.put(N_Car_VIN1_Activity.this, Constant.VIN_Code, shju);
                        SpfUtils.put(N_Car_VIN1_Activity.this, Constant.VIN_InputSucess, "true");
                        if (Inpue_Obu) {
                            startActivity(new Intent(N_Car_VIN1_Activity.this, N_Car_OBU2_Activity.class));
                        } else {
                            startActivity(new Intent(N_Car_VIN1_Activity.this, N_QRSBXX3_Activity.class));
                        }


                        finish();

                    }
                }).show();
                break;

        }
    }

    public static final int REQUEST_CODE_SCAN = 0X01;
    public static final int REQUEST_CODE_PHOTO = 0X02;
    String string = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case REQUEST_CODE_SCAN:

                    break;

            }

        }
    }

}
